﻿using System;
using System.Configuration;
using System.Web.UI;
using com.huigo.Admin.Web.config;
using com.huigo.Admin.Web.Config;
using com.huigo.Admin.Web.ctrl;
using com.huigo.Framework.Passport;

namespace com.huigo.Admin.Web
{
    public class BasePage : System.Web.UI.Page
    {
        MenuConfig mc;

        #region 窗体加载
        protected override void OnLoad(EventArgs e)
        {
            //#if !DEBUG
            // 判断用户登录
            if (!UserPassport.ChkCookieLogin())
            {
                string url = string.Empty;
                try
                {
                    url = Request.Url.AbsoluteUri;
                    url = url.Replace("&", "{$}");
                }
                catch { }

                if (url == string.Empty)
                    Response.Redirect("/Login.aspx");
                else
                    Response.Redirect(string.Format("/Login.aspx?surl={0}", com.loto.Common.Utils.UrlEncode(url)));
            }
            //#endif
            if (base.Master != null)
            {
                Control menuControl = base.Master.FindControl("topmenu");
                if (menuControl != null && menuControl is TopMenu)
                {
                    ((TopMenu)menuControl).Username = this.UserName;
                }
            }
            else
            {
                if (!BaseConfig.HasPageAccess(this.UserName))
                {
                    com.loto.Common.MessageBox.ShowAndRedirect(this.Page, "你没有访问该页面的权限，请联系系统管理员！", "/Login.aspx");
                }
            }
            base.OnLoad(e);
        }
        #endregion

        protected string UserName
        {
            get
            {
                //#if DEBUG
                //                return "admin";
                //#else
                //return UserPassport.GetCookieUsername(UserPassport.EncodingType.UTF8);
                return UserPassport.GetCookieUserName();
                //#endif
            }
        }

        protected long UserId
        {
            get
            {
                //#if DEBUG
                //                return new Guid("3A8A7052-BB3B-42F0-B55A-508C7AFF9135");
                //#else
                return UserPassport.GetCookieUserId();
                //#endif
            }
        }

        protected string UName
        {
            get
            {
                //#if DEBUG
                //                return new Guid("3A8A7052-BB3B-42F0-B55A-508C7AFF9135");
                //#else
                return UserPassport.GetCookieUName();
                //#endif
            }
        }
        protected string imgUrl
        {
            get
            {
                return ConfigurationManager.AppSettings["ImageUrl"];
            }
        }
        public BasePage()
        {
        }

        protected void GoTo(GoToType type)
        {            
            string url=string.Empty;
            switch ((int)type)
            {
                case (int)GoToType.ADDSUC:
                    url = "~/ShowMsg.aspx?msg=操作成功！";
                    break;
                case (int)GoToType.ADDFAIL:
                    url = "~/ShowMsg.aspx?msg=操作失败！";
                    break;
            }
            if (!string.IsNullOrEmpty(url))
                Response.Redirect(url);
        }

        protected enum GoToType
        {
            ADDSUC,
            ADDFAIL
        }

        protected bool VerifyField(FieldType f,string v,bool isMust)
        {
            bool isVerified = true;
            string vv = com.loto.Common.Utils.SqlEscape(v);
            if (isMust && string.IsNullOrEmpty(vv.Trim()))
            {
                isVerified = false;
                return isVerified;
            }
            switch ((int)f)
            {
                case (int)FieldType.NUM:
                    if (!com.loto.Common.Utils.IsInt(vv))
                        isVerified = false;
                    break;
                case (int)FieldType.MONEY:
                    if (!com.loto.Common.Utils.IsDouble(vv))
                        isVerified = false;
                    break;
                case (int)FieldType.STRING:
                    break;
                case (int)FieldType.DATE:
                    if (!com.loto.Common.Utils.IsDate(vv))
                        isVerified = false;
                    break;
            }
            return isVerified;
        }

        protected enum FieldType
        {
            NUM,
            MONEY,
            STRING,
            DATE
        }
        public decimal ConvertCurrencyPay(decimal currency)
        {
            /*舍分处理
            float f = (float)System.Math.Floor(currency);
            float d = currency - f;
            if (d.ToString().Length > 2)
            {
                return f+float.Parse(d.ToString().Substring(0, 3));
            }
            return currency;
            */
            decimal concurrency = (decimal)Math.Round(currency, 2);
            decimal thridCurrency = (decimal)Math.Round(currency, 3);
            if (concurrency - thridCurrency < 0)
            {
                concurrency = concurrency + 0.01M;
            }
            return (decimal)Math.Round(concurrency, 2);
        }
        //public string ServiceAcc
        //{
        //    get { return ConfigurationManager.AppSettings["ACC"]; }
        //}

        //public string ServicePin
        //{
        //    get { return ConfigurationManager.AppSettings["PIN"]; }
        //}

        //private string RequestEntryService(Dict.ERequestFunc funcName, List<RequestFuncParams> paramsList)
        //{
        //    if (paramsList.Count > 0)
        //    {
        //        RequestFunArgs rAddCart = new RequestFunArgs();
        //        foreach (RequestFuncParams p in paramsList)
        //        {
        //            rAddCart.RequestArgList.Add(new RequestArg(p.Name, p.Value));
        //        }
        //        rAddCart.sFuncName = DictEnum.GetDictValueByKey(Dict.DRequestFunc, (int)funcName);
        //        return RequestEntryService(RequestXml.GetRequestXml(rAddCart));
        //    }
        //    else
        //        return string.Empty;
        //}

        //private string RequestEntryService(string req)
        //{
        //    RequestService requestService = new RequestService();
        //    return requestService.RequestEntryService(req, this.ServiceAcc, this.ServicePin);
        //}

        //private ParserXml ParserEntryService(string req)
        //{
        //    RequestService requestService = new RequestService();
        //    return new ParserXml(RequestEntryService(req));
        //}
    }
}
